﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Common;
using JM.JCY.Common.Utilities;
using Microsoft.SharePoint;

namespace JM.JCY.SharePointComponent.LawArchives.LawCaseMaterialDetails
{
    public partial class LawCaseMaterialDetailsUserControl : UserControl
    {
        public int _caseId
        {
            set { ViewState["_caseId"] = value; }
            get { return (int)ViewState["_caseId"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (LawCaseProxy proxy = new LawCaseProxy())
                {
                    if (Request.QueryString["CaseId"] != null)
                    {
                        _caseId = Int32.Parse(Request.QueryString["CaseId"].ToString());
                    }
                    // load data
                    var loadDT = proxy.GetLawCaseMaterial(_caseId, string.Empty);
                    this.lblTitle.Text = loadDT[0].Year + "年扣押冻结物品情况一览表";
                    LoadForEdit(loadDT);
                    LoadForView(loadDT);
                    Session["EditLawCaseMaterialDT"] = loadDT;
                }

            }

            // switch controls
            if (this.hfIsEdit.Value != "1")
                SwitchControls(true, false);
            else
                SwitchControls(false, true);
        }

        private void LoadForView(LawCaseDataSet.T_LawCaseMaterialDataTable dt)
        {
            var editRow = dt[0];

            this.lblBuildDate.Text = editRow.BuildDate.ToString(JCYConstant.Date_Format_CN);
            this.lblHandDate.Text = editRow.HandDate.ToString(JCYConstant.Date_Format_CN);
            this.lblHandleDate.Text = editRow.HandleDate.ToString(JCYConstant.Date_Format_CN);
            this.lblReturnDate.Text = editRow.ReturnDate.ToString(JCYConstant.Date_Format_CN);
            this.lblTurnOverDate.Text = editRow.TurnOverDate.ToString(JCYConstant.Date_Format_CN);
            this.lblFillDept.Text = editRow.FillDept;
            this.lblFiller.Text = editRow.Filler;
            this.lblHandBy.Text = editRow.HandBy;
            this.lblHandlelSituation.Text = editRow.HandlelSituation;
            this.lblInvolvedBy.Text = editRow.InvolvedBy;
            this.lblOthers.Text = editRow.Others;
            this.lblReceiveBy.Text = editRow.ReceiveBy;
            this.lblRemark.Text = editRow.Remark;
            this.lblStuffCount.Text = editRow.StuffCount.ToString();
            this.lblStuffName.Text = editRow.StuffName;
            this.lblYear.Text = editRow.Year;
        }

        private void LoadForEdit(LawCaseDataSet.T_LawCaseMaterialDataTable dt)
        {
            var editRow = dt[0];

            this.txtFillDept.Text = editRow.FillDept;
            this.txtFiller.Text = editRow.Filler;
            this.txtHandBy.Text = editRow.HandBy;
            this.txtHandlelSituation.Text = editRow.HandlelSituation;
            this.txtInvolvedBy.Text = editRow.InvolvedBy;
            this.txtOthers.Text = editRow.Others;
            this.txtReceiveBy.Text = editRow.ReceiveBy;
            this.txtRemark.Text = editRow.Remark;
            this.txtStuffCount.Text = editRow.StuffCount.ToString();
            this.txtStuffName.Text = editRow.StuffName;
            this.txtYear.Text = editRow.Year;

            this.hfBuildDate.Value = editRow.BuildDate.ToString();
            this.hfHandDate.Value = editRow.HandDate.ToString();
            this.hfHandleDate.Value = editRow.HandleDate.ToString();
            this.hfReturnDate.Value = editRow.ReturnDate.ToString();
            this.hfTurnOverDate.Value = editRow.TurnOverDate.ToString();
        }

        private void SwitchControls(bool isLabelsShow, bool isControlsShow)
        {
            // labels
            this.lblBuildDate.Visible = isLabelsShow;
            this.lblFillDept.Visible = isLabelsShow;
            this.lblFiller.Visible = isLabelsShow;
            this.lblHandBy.Visible = isLabelsShow;
            this.lblHandDate.Visible = isLabelsShow;
            this.lblHandleDate.Visible = isLabelsShow;
            this.lblHandlelSituation.Visible = isLabelsShow;
            this.lblInvolvedBy.Visible = isLabelsShow;
            this.lblOthers.Visible = isLabelsShow;
            this.lblReceiveBy.Visible = isLabelsShow;
            this.lblRemark.Visible = isLabelsShow;
            this.lblReturnDate.Visible = isLabelsShow;
            this.lblStuffCount.Visible = isLabelsShow;
            this.lblStuffName.Visible = isLabelsShow;
            this.lblTurnOverDate.Visible = isLabelsShow;
            this.lblYear.Visible = isLabelsShow;

            //controls
            this.txtFillDept.Visible = isControlsShow;
            this.txtFiller.Visible = isControlsShow;
            this.txtHandBy.Visible = isControlsShow;
            this.txtHandlelSituation.Visible = isControlsShow;
            this.txtInvolvedBy.Visible = isControlsShow;
            this.txtOthers.Visible = isControlsShow;
            this.txtReceiveBy.Visible = isControlsShow;
            this.txtRemark.Visible = isControlsShow;
            this.txtStuffCount.Visible = isControlsShow;
            this.txtStuffName.Visible = isControlsShow;
            this.txtYear.Visible = isControlsShow;
            this.dtBuildDate.Visible = isControlsShow;
            this.dtHandDate.Visible = isControlsShow;
            this.dtHandleDate.Visible = isControlsShow;
            this.dtReturnDate.Visible = isControlsShow;
            this.dtTurnOverDate.Visible = isControlsShow;
            this.hr8.Visible = isControlsShow;
            this.TBSubmit.Visible = isControlsShow;

        }

        protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["EditLawCaseMaterialDT"] != null)
            {
                var editDT = (LawCaseDataSet.T_LawCaseMaterialDataTable)Session["EditLawCaseMaterialDT"];

                using (LawCaseProxy proxy = new LawCaseProxy())
                {
                    var editRow = editDT[0];

                    editRow.HandBy = this.txtHandBy.Text;
                    editRow.HandDate = this.dtHandDate.SelectedDate;
                    editRow.StuffName = this.txtStuffName.Text;
                    editRow.StuffCount = Int32.Parse(this.txtStuffCount.Text);
                    editRow.InvolvedBy = this.txtInvolvedBy.Text;
                    editRow.BuildDate = this.dtBuildDate.SelectedDate;
                    editRow.HandlelSituation = this.txtHandlelSituation.Text;
                    editRow.HandleDate = this.dtHandleDate.SelectedDate;
                    editRow.TurnOverDate = this.dtTurnOverDate.SelectedDate;
                    editRow.ReturnDate = this.dtReturnDate.SelectedDate;
                    editRow.ReceiveBy = this.txtReceiveBy.Text;
                    editRow.Others = this.txtOthers.Text;
                    editRow.Remark = this.txtRemark.Text;

                    editRow.FillDept = this.txtFillDept.Text;
                    editRow.Filler = this.txtFiller.Text;
                    editRow.Year = this.txtYear.Text;

                    JCYUtility.UpdateCommonFields(editRow, "Iori");
                    proxy.UpdateLawCaseMaterial(editDT);
                    Response.Redirect("LawCaseMaterial.aspx");
                }
            }
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("LawCaseMaterial.aspx");
        }

        protected void lkbEditLawCaseMaterial_Click(object sender, EventArgs e)
        {
            this.hfIsEdit.Value = "1";
            SwitchControls(false, true);
            this.dtBuildDate.SelectedDate = Convert.ToDateTime(this.hfBuildDate.Value);
            this.dtHandDate.SelectedDate = Convert.ToDateTime(this.hfHandDate.Value);
            this.dtHandleDate.SelectedDate = Convert.ToDateTime(this.hfHandleDate.Value);
            this.dtReturnDate.SelectedDate = Convert.ToDateTime(this.hfReturnDate.Value);
            this.dtTurnOverDate.SelectedDate = Convert.ToDateTime(this.hfTurnOverDate.Value);
        }

    }
}
